package com.zsr.example.controller.front;

import com.alibaba.fastjson.JSONObject;
import com.zsr.example.config.exception.CommonJsonException;
import com.zsr.example.dto.session.WebsocketMessageDTO;
import com.zsr.example.entity.EduComment;
import com.zsr.example.entity.UcenterMember;
import com.zsr.example.service.EduCommentService;
import com.zsr.example.service.impl.WebSocketServiceImpl;
import com.zsr.example.util.CommonUtil;
import com.zsr.example.util.JwtUtil;
import com.zsr.example.util.constants.ChatTypeEnum;
import com.zsr.example.util.constants.CommentConstants;
import com.zsr.example.util.constants.ErrorEnum;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * 前台评论控制器
 *
 * @author zsr
 * @date 2021/6/22 17:21
 */
@CrossOrigin
@RestController
@RequestMapping("/eduservice/commentfront")
public class CommentController {

    @Autowired
    private EduCommentService commentService;

    @Autowired
    private WebSocketServiceImpl webSocketService;

    /**
     * 根据课程id查询评论列表
     *
     * @param page
     * @param limit
     * @param targetType
     * @param targetId
     * @return
     */
    @GetMapping("{page}/{limit}")
    public JSONObject index(
            @PathVariable Long page,
            @PathVariable Long limit,
            int targetType,
            String targetId) {
        return CommonUtil.successJson(commentService.commentPageList(page, limit, targetType, targetId));
    }

   /* @GetMapping("test/{id}/{msg}")
    public JSONObject test(@PathVariable String id, @PathVariable String msg) {
        try {
            WebsocketMessageDTO messageDTO = WebsocketMessageDTO.builder()
                    .data(msg)
                    .type(ChatTypeEnum.SEND_MESSAGE.getType())
                    .build();
            webSocketService.sendMessageTo(JSONObject.toJSONString(messageDTO), id);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return CommonUtil.successJson();

    }*/

    /**
     * 添加评论
     *
     * @param comment
     * @param request
     * @return
     */
    @PostMapping("save")
    public JSONObject save(@RequestBody EduComment comment, HttpServletRequest request) {
        String memberId = JwtUtil.getMemberIdByJwtToken(request);
        if (StringUtils.isEmpty(memberId)) return CommonUtil.errorJson(ErrorEnum.E_20011);

        UcenterMember fromUser = commentService.commentSave(comment, memberId);
        //ws通知
        if (comment.getType()!= CommentConstants.TYPE_COM
                && !comment.getFromMemberId().equals(comment.getToMemberId())) {
            try {

                WebsocketMessageDTO messageDTO = WebsocketMessageDTO.builder()
                        .data("您收到@"+fromUser.getNickname()+"的回复消息")
                        .type(ChatTypeEnum.COMMENTS_NOTICE.getType())
                        .build();
                webSocketService.sendMessageTo(JSONObject.toJSONString(messageDTO), comment.getToMemberId());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return CommonUtil.successJson();
    }

    @GetMapping("del/{id}")
    public JSONObject del(@PathVariable String id, HttpServletRequest request) {
        String memberId = JwtUtil.getMemberIdByJwtToken(request);
        if (StringUtils.isEmpty(memberId)) return CommonUtil.errorJson(ErrorEnum.E_20011);

        EduComment comment = commentService.getById(id);
        if (ObjectUtils.isEmpty(comment)) throw new CommonJsonException(ErrorEnum.E_90004);
        if (!comment.getFromMemberId().equals(memberId))
            throw new CommonJsonException(ErrorEnum.E_90005);

        commentService.removeById(id);
        return CommonUtil.successJson();
    }

    /*@GetMapping("test/{s}")
    public JSONObject test(@PathVariable String s) {
        String filter = sensitiveWordFilter.replaceSensitiveWord(s, 1, "*");
        return CommonUtil.successJson(filter);

    }*/

}
